package com.yawn.zerodefect.controller;

import com.yawn.zerodefect.controller.entity.RestResponse;
import com.yawn.zerodefect.dao.ProjectDao;
import com.yawn.zerodefect.entity.Project;
import com.yawn.zerodefect.service.ProjectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController {

    @Resource
    private ProjectService projectService;

    @GetMapping("/getOne")
    public RestResponse getOne(String code) {
        Project project = projectService.findOne(code);
        return RestResponse.newInstance(1, project);
    }

    @GetMapping("/getAll")
    public RestResponse getAll() {
        List<Project> projectList = projectService.findAll();
        return RestResponse.newInstance(1, projectList);
    }

    @PostMapping("/add")
    public RestResponse add(@RequestBody Project project) {
        projectService.save(project);
        return RestResponse.newInstance(1);
    }

    @PostMapping("/delete")
    public RestResponse delete(@RequestBody Project pro) {
        projectService.delete(pro);
        return RestResponse.newInstance(1);
    }

    @PostMapping("/prefer")
    public RestResponse setPreferPro(@RequestBody Project pro) {
        return projectService.setPreferPro(pro.getCode());
    }

    @GetMapping("/prefer")
    public RestResponse getPreferPro() {
        return projectService.getPreferPro();
    }
}
